package xj.graph2d.command;

import java.awt.geom.Rectangle2D;

import xj.graph2d.DrawShape;
import xj.graph2d.GraphDocument;
import xj.graph2d.GraphNodeBase;
import xj.graph2d.GraphView;

public class AddNodeCommand extends AbstractCommand {

  public AddNodeCommand(GraphView view, GraphDocument doc, GraphNodeBase shape) {
    super(view, doc);
    this.shape = shape;
    graph = doc;
  }

  public void execute() {
    if (shape != null) {
      DrawShape selected = doc.getSelectedShape();
      doc.setSelectedShape(null);
      canvas.repaintShape(selected);
      graph.addNode(shape);
      // graph.setSelectedShape(shape);
      canvas.repaintShape(shape);
    }
  }

  public void undo() {
    if (shape != null) {
      graph.removeNode(shape);

      Rectangle2D r2 = shape.getBounds();
      canvas.repaintRect(r2, true, true);
    }
  }

  protected GraphNodeBase shape;

  protected GraphDocument graph;

}
